package com.deepcode.StringProcess.template;


import com.deepcode.StringProcess.strategy.CharacterReplacementStrategy;
import com.deepcode.StringProcess.struct.CharLinkedList;

public abstract class AbstractStringProcessor implements IntfStringProcessor {
    protected CharacterReplacementStrategy strategy;

    public AbstractStringProcessor(CharacterReplacementStrategy strategy) {
        System.out.println("use strategy------: " + strategy.getClass().getSimpleName());
        this.strategy = strategy;
    }

    public void setStrategy(CharacterReplacementStrategy strategy) {
        this.strategy = strategy;
    }

    protected abstract boolean processLinkedList(CharLinkedList linkedList);

    /**
     * 处理字符串，将连续3个或更多相同字符替换
     *
     * @param input 输入字符串
     * @return 处理后的字符串
     */
    @Override
    public String processString(String input) {
        if (input == null || input.isEmpty()) {
            return input;
        }

        // 创建双向链表
        CharLinkedList linkedList = new CharLinkedList();
        linkedList.fromString(input);

        // 处理链表
        boolean changed = processLinkedList(linkedList);

        // 递归处理直到没有变化
        if (changed) {
            System.out.println("-> " + linkedList.toString());
            return processString(linkedList.toString());
        }

        return linkedList.toString();
    }

}